package com.gxa.controller.zhy;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.gxa.common.R;
import com.gxa.pojo.System_Config_Param;
import com.gxa.service.SystemSettingsService;
import com.gxa.utils.GenerateSequenceUtil;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * User: 赵贺益
 * Date: 2021/12/04 9:18
 * Description:
 * Version: V1.0
 */
@Controller
@Api(value ="" ,tags = "系统管理模块接口（系统设置）1")
@RequestMapping("/system")
public class SystemSettingsController {
    @Autowired
    private SystemSettingsService systemSettingsService;

    /**
     * 获取系统信息，并显示到表单中
     * @return
     */
    @ResponseBody
    @RequestMapping(value ="/querySystemConfig",method = RequestMethod.GET)
    @ApiOperation(value = "获取系统设置信息的接口",notes = "返回的是一个对象R", httpMethod = "GET", response = R.class)
    @ApiResponses({
            @ApiResponse(code=200,message = "查询成功"),
            @ApiResponse(code=504,message = "查询失败"),
            @ApiResponse(code = 500,message = "后台服务错误")
    })
    public R querySystemConfig(HttpServletRequest request){
        //当前项目的绝对位置
        String absoultePath=request.getServletContext().getRealPath("");
        R r=systemSettingsService.querySystemConfig(absoultePath);
        return  r;
    }

    /**
     * 修改系统设置的接口
     * @param system_config_param
     * @param request
     * @return
     */
//,produces = "application/json;charset=utf-8"
    @ResponseBody
    @RequestMapping(value ="/updateSystemConfig",method = RequestMethod.POST)
    @ApiOperation(value = "修改系统设置信息的接口",notes = "返回的是一个对象R.",httpMethod = "POST", response = R.class)
    @ApiResponses({
            @ApiResponse(code=200,message = "修改成功"),
            @ApiResponse(code=504,message = "修改失败"),
            @ApiResponse(code=501,message = "没有选中图片"),
            @ApiResponse(code=502,message = "图片文件格式错误，允许.jpg.png.gif文件"),
            @ApiResponse(code = 500,message = "后台服务错误")
    })
    public  R updateSystemConfig(@RequestBody System_Config_Param system_config_param, HttpServletRequest request) throws IOException {
         System.out.println(system_config_param);
         R r=systemSettingsService.updateSystemConfig(system_config_param);
         return r;
    }

    /**
     * 上传文件的接口
     */

    @RequestMapping(value ="/uploadLogo",method = RequestMethod.POST)
    @ApiOperation(value = "上传文件的接口",notes = "返回的是一个对象R.",httpMethod = "POST", response = R.class)
    @ApiResponses({
            @ApiResponse(code=200,message = "修改成功"),
            @ApiResponse(code=502,message = "图片文件格式错误，允许.jpg.png.gif文件"),
            @ApiResponse(code = 500,message = "后台服务错误")
    })
    @ResponseBody
    public R uploadLogo( System_Config_Param system_config_param, HttpServletRequest request) {
        R r = new R();
        try {
            System.out.println("上传文件");
            MultipartFile multipartFile = system_config_param.getUser_pic();
            String sourceFilename = multipartFile.getOriginalFilename();
            String ext = sourceFilename.substring(sourceFilename.lastIndexOf("."));
            //上传.jpg.png.gif
            String allowExt = ".jpg|.png|.gif";
            if (allowExt.lastIndexOf(ext) == -1) {
//                /**
//                 * 返回信息提示前端不能上传此种文件类型
//                 *
//                 */
                r.setCode(502);
                r.setMsg("图片文件格式错误，允许.jpg.png.gif文件");
                return r;
            } else {
                String fileName = GenerateSequenceUtil.generateSequenceNo();
                String tarGetFileName = fileName + ext;
//                此时uploadpath为绝对路径   加文件要存的地址和文件夹名字
//                getRealPath为获取该过程的绝对位置
                String uploadpath = request.getServletContext().getRealPath("") + File.separator + "upload" + File.separator;
                File file = new File(uploadpath);
                if (!file.exists()) {
                    file.mkdirs();
                }
                File newFile=new File(uploadpath+tarGetFileName);
                FileCopyUtils.copy(multipartFile.getBytes(),newFile);
                String relativaPath=File.separator+"upload"+File.separator;
                System.out.println(relativaPath);
                System_Config_Param system_config_param1=new System_Config_Param();
                system_config_param1.setLogo(relativaPath+tarGetFileName);
//                System.out.println(system_config_param1);
                r.setData(system_config_param1);
                r.setCode(200);
                r.setMsg("123131");
                return r;
            }
        } catch (Exception e) {
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("123131");
            return r;
        }
    }

}
